package com.db.service.bank;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;

public class BankClient {
    public static void main(String[] args) {
        BankClient bankClient = new BankClient();
        bankClient.startClient();
    }

    private void startClient() {
        Scanner in = new Scanner(System.in);
        try (Socket socket = new Socket("localhost", BankServer.PORT_NUMBER)
        ) {
            try (DataInputStream dataInputStream = new DataInputStream(socket.getInputStream());
                 DataOutputStream dataOutputStream = new DataOutputStream(socket.getOutputStream())
            ) {
                String queue;
                while (!(queue = in.nextLine()).equals("exit")) {
                    dataOutputStream.writeUTF(queue);
                    System.out.println(dataInputStream.readUTF());
                }
            }
        } catch (UnknownHostException e) {
            System.err.println("Unknown host");
            e.printStackTrace();
        } catch (IOException e) {
            System.err.println("IO exception");
            e.printStackTrace();
        }
    }
}
